package com.sushe.service;

import com.sushe.dao.*;
import com.sushe.service.jiekou.RoomSwapService;

import java.sql.SQLException;
import java.util.List;
import java.util.Scanner;

public class RoomSwapServiceImpl implements RoomSwapService {

    private static StudentDao studentDao=new StudentDao();
    private static RoomSwapDao roomSwapDao=new RoomSwapDao();
    private static RoomDao roomDao=new RoomDao();
    private static BedAssignmentDao bedAssignmentDao=new BedAssignmentDao();
    private static BedDao bedDao=new BedDao();

    @Override
    public  void roomSwap() throws SQLException, ClassNotFoundException {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要交换宿舍的学生名称：");
        String stuName=sc.nextLine();
        Integer studentId=studentDao.getStudentId(stuName);
        System.out.println("该学生ID为："+studentId);
        List<Integer> list= roomSwapDao.getBedNumberAndRoomIdByStudentId(studentId);
        if(list.size()==0&&list==null){
            System.out.println("该学生没有入住宿舍信息！");
            return;
        }
        int roomId=list.get(1);
        int bedNumber=list.get(0);

        //获取room_number房间号
        String roomNumber= roomDao.getRoomNumberByRoomId(roomId);
        System.out.println("该学生住在"+roomNumber+"宿舍的"+bedNumber+"床位");
        System.out.println("-------------------------------------");

        //以下调用BedAssignmentService类的功能


        System.out.println("请输入要更换的宿舍楼Id：");
        Integer buildingId=sc.nextInt();
        //判断该楼栋编号是否存在，不存在则报错并返回结束方法
        String buildingName= bedAssignmentDao.selectByBuildingId(buildingId);
        System.out.println("楼栋名称为："+buildingName);
        if(buildingName== null){
            System.out.println("该楼栋不存在");
            return;
        }

        //用户输入房间号判断是否存在
        System.out.println("请输入宿舍房间号：");
        String roomNumber2=sc.next();
        //判断房间是否存在
        Integer roomId2=roomDao.selectByRoomNumberAndBuildingId(roomNumber2,buildingId);
        if(roomId2==null){
            System.out.println("该房间不存在");
            return;
        }

        //枚举所有可分配的床位集合
        List<Integer> list2 =null;
        while(true) {
            System.out.println("请选择分配的床位号：");
            list2=bedAssignmentDao.getEmptyBedNumbersByRoomId(roomId2);
            if(list2==null&&list2.size()==0){
                System.out.println("该房间没有空床位,请重新选择");
                continue;
            }
            if(list2!=null&&list2.size()!=0)
                break;
        }
        System.out.println("该房间所有可分配的床位号为：");
        for(Integer bedNumber2:list2){
            System.out.print(bedNumber2+"  ");
        }
        System.out.println();


        System.out.print("请输入您要选择分配的床位号：");

        int bedNumber3=sc.nextInt();
        for(Integer bedNumber1:list2){
            if(bedNumber3==bedNumber1){

                //删除原有的床位绑定student_id信息
                bedDao.deleteStudentIdByStudentId(studentId);

                //添加新的床位绑定student_id信息
                bedAssignmentDao.addBedAssignment(bedNumber3,roomId2,studentId);
                System.out.println("分配成功！");
                return;
            }
        }
        System.out.println("选择了非法床位！无法分配！");

        //调用BedAssignmentService类功能结束


        return;
    }
}
